package com.ailiwu.www.ailiwu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.ailiwu.www.ailiwu.R;

import java.util.ArrayList;

public class NavigationActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private ArrayList<ImageView> ivList;
    private ArrayList<View> pointList;
    private LinearLayout llPoints;
    private MyPagerAdapter myPagerAdapter;
    private Button btn_enter_main;
    private int currentPos = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        findViews();

        initData();

        //创建ViewPager适配器并进行适配
        myPagerAdapter = new MyPagerAdapter();
        mViewPager.setAdapter(myPagerAdapter);

        registerListener();


    }

    private void registerListener() {
        // 立即体验按钮监听
        btn_enter_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(NavigationActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        // 设置滑动监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // Log.i("oye", "切换到:"+position);
                pointList.get(currentPos).setEnabled(true);
                pointList.get(position).setEnabled(false);
                currentPos = position;
                if (position == ivList.size() - 1) {
                    // 如果滑动到了最后一个页面，就显示按钮
                    btn_enter_main.setVisibility(View.VISIBLE);
                } else {
                    btn_enter_main.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    // 初始化信息
    private void initData() {
        // 初始化导航图片
        ivList = new ArrayList<ImageView>();
        int[] resId = {R.drawable.walkthrough_1,
                R.drawable.walkthrough_2,
                R.drawable.walkthrough_3,
                R.drawable.walkthrough_4};

        for (int i = 0; i < resId.length; i++) {
            ImageView iv = new ImageView(this);
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                    RadioGroup.LayoutParams.MATCH_PARENT,
                    RadioGroup.LayoutParams.MATCH_PARENT);

            iv.setLayoutParams(layoutParams);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setImageResource(resId[i]);

            ivList.add(iv);
        }

        // 初始化圆点
        pointList = new ArrayList<>();
        for (int i = 0; i < resId.length; i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.selector_point);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
            params.setMargins(3,0,3,0);
            point.setLayoutParams(params);
            // 让每个圆点都记录下自己的下标
            pointList.add(point);
            point.setTag(i);
            if (i == 0) {
                // 默认一开始显示的是第一页
                point.setEnabled(false);
            } else {
                point.setEnabled(true);
            }

            // 将圆点添加到布局中
            llPoints.addView(point);
        }
    }

    private void findViews() {
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        llPoints = (LinearLayout) findViewById(R.id.ll_point);
        btn_enter_main = (Button) findViewById(R.id.btn_enter_main);
    }

    /**
     * ViewPager使用的适配器
     */
    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return ivList != null ? ivList.size() : 0;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(ivList.get(position));
            return ivList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ivList.get(position));
        }
    }
}
